﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Winter.BackEnd.Models.PersonBuilder;
using System.Text;
using System.Web.Security;

namespace Winter.BackEnd.Models.Domain
{
    public partial class Person
    {

        #region Fields

        private string userName;

        private string email;

        private string userRole;

        private aspnet_User user;

        private ParameterValue sex;

        #endregion

        #region Properties

        public aspnet_User User
        {
            get
            {
                return user ?? this.aspnet_Users.SingleOrDefault(u => u.PersonID == ID);
            }
        }

        public string UserName
        {
            get
            {
                return userName = User != null ? User.UserName : userName;
            }
            set
            {
                userName = value;
            }
        }

        public string Email
        {
            get
            {
                return email = User != null ? User.aspnet_Membership.Email : email;
            }
            set
            {
                email = value;
            }
        }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }

        public string UserRole
        {
            get
            {
                return userRole = User != null && User.aspnet_UsersInRoles.Count != 0 ?
                    User.aspnet_UsersInRoles.First().aspnet_Role.RoleName : "Anonymous"; ;
            }
            set
            {
                userRole = value;
            }
        }

        public string FullName
        {
            get
            {
                return string.Format("{0}{1} {2}.{3}.",
                        !string.IsNullOrEmpty(this.LastName) ? this.LastName[0].ToString().ToUpper() : "",
                        !string.IsNullOrEmpty(this.LastName) ? this.LastName.Substring(1) : "",
                        !string.IsNullOrEmpty(this.FirstName) ? this.FirstName[0].ToString().ToUpper() : "",
                        !string.IsNullOrEmpty(this.SecondName) ? this.SecondName[0].ToString().ToUpper() : "");
            }
        }

        public ParameterValue Sex
        {
            get
            {
                return Repository.CurrentDataContext.ParameterValues.SingleOrDefault(p => p.ID == SexID);
            }
            set
            {
                sex = value;
            }
        }

        public IPerson PersonInRole
        {
            get
            {

                if (Roles.IsUserInRole(UserRoles.Student.RoleName))
                {
                    return this.Students.First();
                }
                else if (Roles.IsUserInRole(UserRoles.Admin.RoleName))
                {
                    return new Admin();
                }
                return Students.First();
            }
        }
        #endregion

        public void Construct(PersonBuilder.PersonBuilder personBuilder)
        {
            personBuilder.BuildPersonDetails();
            personBuilder.BuildUserRole();
            personBuilder.BuildPersonType();
            personBuilder.BuildPersonRelationShips();
        }

    }
}
